Python String capitalize() (With Examples)

您所在的位置:网站首页 python uppercase Python String capitalize() (With Examples)

Python String capitalize() (With Examples)

#Python String capitalize() (With Examples)| 来源: 网络整理| 查看: 265

The capitalize() method converts the first character of a string to an uppercase letter and all other alphabets to lowercase.

Example sentence = "i love PYTHON" # converts first character to uppercase and others to lowercase capitalized_string = sentence.capitalize() print(capitalized_string) # Output: I love python capitalize() Syntax

The syntax of the capitalize() method is:

string.capitalize() capitalize() Parameter

The capitalize() method doesn't take any parameter.

capitalize() Return Value

The capitalize() method returns:

a string with the first letter capitalized and all other characters in lowercase. Example 1: Python capitalize() sentence = "python is AWesome." # capitalize the first character capitalized_string = sentence.capitalize() print(capitalized_string)

Output

Python is awesome.

In the above example, we have used the capitalize() method to convert the first character of the sentence string to uppercase and the other characters to lowercase.

Here, sentence.capitalize() returns "Python is awesome" which is assigned to capitalized_string.

Example 2: capitalize() Doesn't Change the Original String

The capitalize() method returns a new string and doesn't modify the original string. For example:

sentence = "i am learning PYTHON." # capitalize the first character capitalized_string = sentence.capitalize() # the sentence string is not modified print('Before capitalize():', sentence) print('After capitalize():', capitalized_string)

Output

Before capitalize(): i am learning PYTHON. After capitalize(): I am learning python.

Here, the capitalize() method doesn't modify the original sentence string.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3